package joodle.ui;
/**
 * Represents the "Load" action on menu bar
 */
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.Collection;

import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.filechooser.FileNameExtensionFilter;

import joodle.domain.Canvas;
import joodle.domain.Shape;
import joodle.si.IOManager;
import joodle.si.ResourceManager;

@SuppressWarnings("serial")
public class SVGLoadAction extends AbstractAction {

	public SVGLoadAction() {
		super(ResourceManager.message("LOAD"), ResourceManager.makeIcon("open"));
		putValue(MNEMONIC_KEY, KeyEvent.VK_L);
		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control L"));
	}
	/**
	 * @see AbstractAction#actionPerformed(ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		JFileChooser fc = new JFileChooser();
		FileNameExtensionFilter ff = new FileNameExtensionFilter("Scalable Vector Graphics (SVG)", "svg");
		fc.setFileFilter(ff);
		if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
			File f = fc.getSelectedFile();
			try {
				Collection<Shape> ss = IOManager.loadXML(f);
				Canvas.instance().clear();
				Canvas.instance().addShapes(ss);
				CanvasComponent.instance().repaint();
			} catch (Exception e) {
				JOptionPane.showMessageDialog(fc,
						"Joodle encountered an error while loading!", "Error",
						JOptionPane.ERROR_MESSAGE);
			}
		}
		
	}

}
